import Vue from 'vue'
import Router from 'vue-router'
import login from '@/components/login/login'
import home from '@/components/home/home'
import userList from '@/components/user/userList'
import roles from '@/components/roles/roles.vue'
import rights from '@/components/rights/rights.vue'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: login
    },
    {
      path: '/home',
      component: home,
      children: [
        { path: '/users', component: userList },
        { path: '/roles', component: roles },
        { path: '/rights', component: rights }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    let token = sessionStorage.getItem('token')
    if (token) {
      next()
    } else {
      next(from.path)
    }
  }
})

export default router
